﻿using System.Collections.Generic;

namespace Ex03.GarageLogic
{
    public abstract class Truck : Vehicle
    {
        private const string k_MaximumPassengersKey = "MaximumPassengers";
        private const string k_IsHazmatKey = "IsHazmat";
        private const int k_NumberOfWheels = 6;
        private const float k_MaximumAirPressure = 28.0f;

        private int m_MaximumPassengers;
        private bool m_IsHazmat;

        protected Truck()
        {
            MaximumPassengers = 0.ToString();
            IsHazmat = false.ToString();
        }

        public override void ParseFields(Dictionary<string, string> i_Form)
        {
            base.ParseFields(i_Form);

            string maximumPassengers;
            if (!i_Form.TryGetValue(Truck.k_MaximumPassengersKey, out maximumPassengers))
            {
                throw new KeyNotFoundException(Truck.k_MaximumPassengersKey);
            }

            string isHazmat;
            if (!i_Form.TryGetValue(Truck.k_IsHazmatKey, out isHazmat))
            {
                throw new KeyNotFoundException(Truck.k_IsHazmatKey);
            }

            MaximumPassengers = maximumPassengers;
            IsHazmat = isHazmat;
            Wheels = ParseWheels(i_Form, k_NumberOfWheels, k_MaximumAirPressure);
        }

        public override Dictionary<string, string> CreateForm()
        {
            Dictionary<string, string> form = base.CreateForm();
            form.Add(k_MaximumPassengersKey, null);
            form.Add(k_IsHazmatKey, null);

            Wheel.CreateForm(ref form, k_NumberOfWheels);
            
            return form;
        }

        public string MaximumPassengers
        {
            get { return m_MaximumPassengers.ToString(); }
            set 
            { 
                try
                {
                    m_MaximumPassengers = int.Parse(value);
                }
                catch (System.FormatException)
                {
                    throw new System.FormatException(Truck.k_MaximumPassengersKey);
                }
            }
        }

        public string IsHazmat
        {
            get { return m_IsHazmat.ToString(); }
            set 
            {
                try
                {
                    m_IsHazmat = bool.Parse(value);
                }
                catch (System.FormatException)
                {
                    throw new System.FormatException(Truck.k_IsHazmatKey);
                }
            }
        }

        public override string ToString()
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            stringBuilder.Append(base.ToString());
            stringBuilder.Append("Maximum Passengers: ");
            stringBuilder.AppendLine(MaximumPassengers.ToString());
            stringBuilder.Append("Is Hazmat: ");
            stringBuilder.AppendLine(IsHazmat.ToString());

            return stringBuilder.ToString();
        }
    }
}
